﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class DanhMucKhachHang : Form
    {
        List<NhanVien> danhSachNhanVien;
        List<KhachHang> danhSachKhachHang;

        public DanhMucKhachHang()
        {
            InitializeComponent();
        }

        private void DanhMucKhachHang_Load(object sender, EventArgs e)
        {
            LoadDanhSachNhanVien();

            LoadDanhSachKhachHang();
        }

        private void LoadDanhSachNhanVien()
        {
            danhSachNhanVien = Business.NhanVien.LayDanhSach();
            cbxNVQuanli.DataSource = danhSachNhanVien;

            cbxNVQuanli.DisplayMember = "TenNV";

            cbxNVQuanli.ValueMember = "MaNVHienThi";
            cbxNVQuanli.SelectedItem = null;
        }

        private void LoadDanhSachKhachHang()
        {
            
            danhSachKhachHang = Business.KhachHang.LayDanhSach();
            gridviewDachsachKH.DataSource = danhSachKhachHang;

            gridviewDachsachKH.Columns["MaNV"].Visible = false;
            gridviewDachsachKH.Columns["MaKH"].Visible = false;
            
            gridviewDachsachKH.Columns["MaKH"].Visible = false;
            gridviewDachsachKH.Columns["MaKHHienThi"].HeaderText = "Mã KH";
            gridviewDachsachKH.Columns["TenKH"].HeaderText = "Tên KH";

            gridviewDachsachKH.Columns["DiaChi"].HeaderText = "Địa chỉ";
            gridviewDachsachKH.Columns["DienThoai"].HeaderText = "Điện thoại";
            gridviewDachsachKH.Columns["Email"].HeaderText = "Email";

            gridviewDachsachKH.Columns["Fax"].HeaderText = "Fax";

            gridviewDachsachKH.Columns["MaSoThue"].HeaderText = "Mã số thuế";
            gridviewDachsachKH.Columns["TenNV"].HeaderText = "NV quản lí";
        }

        private void gridviewDachsachKH_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
            if (danhSachKhachHang != null) 
            {
                KhachHang kh = danhSachKhachHang[e.RowIndex];

                txtMaKH.Text = kh.MaKHHienThi;
                txtTenKH.Text = kh.TenKH;
                
                cbxNVQuanli.Text = kh.TenNV;
                txtDiaChi.Text = kh.DiaChi;

                txtDienThoai.Text = kh.DienThoai;
                txtFax.Text = kh.Fax;
                txtMaSoThue.Text = kh.MaSoThue;

                txtEmail.Text = kh.Email;

            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            txtMaKH.Text = "";
            txtTenKH.Text = "";
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtFax.Text = "";
            txtEmail.Text = "";
            txtMaSoThue.Text = "";
            cbxNVQuanli.Text = "";
            txtTenKH.Focus();

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (KiemTra() == false)
                return;
            
            if (txtMaKH.Text == "")
            {

                KhachHang.ThemMoi(new KhachHang(txtTenKH.Text, txtDiaChi.Text, txtDienThoai.Text, txtEmail.Text,
                    txtFax.Text, txtMaSoThue.Text, cbxNVQuanli.SelectedValue.ToString()));               

            }
            else
            {

                KhachHang.CapNhat(new KhachHang( txtMaKH.Text,txtTenKH.Text, txtDiaChi.Text, txtDienThoai.Text, txtEmail.Text,
                    txtFax.Text, txtMaSoThue.Text, cbxNVQuanli.SelectedValue.ToString()));
                
            }
            LoadDanhSachKhachHang();

        }
        private bool KiemTra()
        {


            if (txtTenKH.Text == "" || txtDiaChi.Text == "" || txtDienThoai.Text == "" ||
                //txtNgaySinh.Text==""||
                cbxNVQuanli.Text=="")
            {
                MessageBox.Show("Nhập đầy đủ thông tin!");
               txtTenKH.Focus();
                return false;
            }
            return true;
        }
    }
}
